package cn.softgame.jserver.application.connection;

import cn.softgame.jserver.application.Connection;
import cn.softgame.jserver.connector.udp.UDPClient;
import cn.softgame.jserver.queue.QueueFactory;

/**
 * UDP不需要连接，直接发送和接受，因此发送和接受数据包比TCP快，
 * 但是，容易丢包，在丢包的情况下，需要使用TCP连接重发。
 * @author xuquan
 * 日期:2013-5-28
 */
public class UdpConnection extends Connection {
	private final static QueueFactory queueFactory = QueueFactory.getInstance();
	private UDPClient client=null;
	
	/**
	 * 客户端连接初始化
	 * @param client
	 */
	public UdpConnection(UDPClient client){
		this();
		this.client=client;
		this.csType=ConnectionFactory.CLIENT;
	}
	
	/**
	 * 服务器端连接初始化
	 */
	public UdpConnection(){
		queue = queueFactory.getQueue(QueueFactory.UDP_QUEUE);
		this.csType=ConnectionFactory.UDPSERVER;
	}
	
	@Override
	public void open(String url, int port) throws Exception {
		client.openDatagramChannel(url, port);
	}

	@Override
	public void close() throws Exception {
		try{
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public boolean isConnect() throws Exception {
		return true;
	}

}
